package com.zhs.gccs.view.taskMap;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.zhs.gccs.R;
import com.zhs.gccs.adapter.PoiAdapter;
import com.zhs.gccs.model.PoiModel;
import com.zhs.gccs.utils.GPSUtils;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.SoftKeyboardUtils;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.view.BaseView;

import java.util.ArrayList;
import java.util.List;

public class PoiSearchView extends BaseView {
    private EditText et_poi;
    private View ll_close;
    private View ll_search;
    PoiSearch mPoiSearch;
    private RecyclerView rv_search;// 项目列表
    private PoiAdapter poiAdapter;

    private List<PoiModel> poiModels;//项目
    private String city;



    public void setCity(String city) {
        this.city = city;
    }

    public PoiSearchView(@NonNull Context context) {
        super(context);
    }

    public PoiSearchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PoiSearchView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        View searchResultView = LayoutInflater.from(context).inflate(R.layout.layout_poi_search_result,
                this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        mPoiSearch = PoiSearch.newInstance();
        rv_search = searchResultView.findViewById(R.id.rv_search);
        this.poiModels = new ArrayList<>();
        rv_search.setLayoutManager(new LinearLayoutManager(context));
        poiAdapter = new PoiAdapter(context, this.poiModels);
        rv_search.setAdapter(poiAdapter);
        poiAdapter.setOnItemListener(new PoiAdapter.OnItemListener() {
            @Override
            public void onItemClick(int position) {
                if(searchViewListener!=null){
                    searchViewListener.moveTo(poiModels.get(position));
                }
            }
        });

        et_poi = searchResultView.findViewById(R.id.et_poi);
        et_poi.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(TextUtils.isEmpty(charSequence)){
                    ll_search.setVisibility(VISIBLE);
                    ll_close.setVisibility(GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        ll_search = searchResultView.findViewById(R.id.ll_search);
        ll_search.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (TextUtils.isEmpty(et_poi.getText().toString())) {
                    poiModels.clear();
                    poiAdapter.notifyDataSetChanged();
                    return;
                }
                if(TextUtils.isEmpty(city)){
                    ToastUtils.toast("请开启定位");
                    return;
                }
                SoftKeyboardUtils.hideSoftInput(context,et_poi);
                mPoiSearch.searchInCity(new PoiCitySearchOption()
                        .city(city) //必填
                        .keyword(et_poi.getText().toString()) //必填
                        .pageCapacity(30)
                        .pageNum(0));
                ll_search.setVisibility(GONE);
                ll_close.setVisibility(VISIBLE);
            }
        });
        ll_close = searchResultView.findViewById(R.id.ll_close);
        ll_close.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                et_poi.setText("");
                poiModels.clear();
                poiAdapter.notifyDataSetChanged();
                ll_search.setVisibility(VISIBLE);
                ll_close.setVisibility(GONE);
                if(searchViewListener!=null)searchViewListener.cloneSearchPoi();;
            }
        });
        OnGetPoiSearchResultListener listener = new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                poiModels.clear();
                if (poiResult != null && poiResult.getAllPoi() != null) {
                    List<PoiInfo> allPoi = poiResult.getAllPoi();
                    Stream.of(allPoi).forEach(item -> {
                        PoiModel model = new PoiModel();
                        model.setAddress(item.getAddress());
                        model.setCity(item.getCity());
                        model.setProvince(item.getProvince());
                        model.setName(item.getName());
                        LatLng location = item.getLocation();
                        if(location!=null){
                            double[] lonlat = GPSUtils.gcj02towgs84(location.longitude, location.latitude);
                            model.setLon(lonlat[0]);
                            model.setLat(lonlat[1]);
                        }
                        poiModels.add(model);
                    });
                }
                poiAdapter.notifyDataSetChanged();
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }

            //废弃
            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }
        };
        mPoiSearch.setOnGetPoiSearchResultListener(listener);
    }


    private PoiSearchView.SearchResultViewListener searchViewListener;

    public void setSearchViewListener(PoiSearchView.SearchResultViewListener searchViewListener) {
        this.searchViewListener = searchViewListener;
    }

    public interface SearchResultViewListener {
        void moveTo(PoiModel poiModel);
        void cloneSearchPoi();
    }
}
